package com.ys.controller.api.logistics;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.logistics.TlDecCustomSub;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/*
 * 报关信息管理
 */
@Controller
@RequestMapping("/api/inter/tlDecCustom")
public class ApiInterTlDecCustomController {
	
	@Autowired
	private CommonApiService<TlDecCustomSub> commonApiService;

	@RequiresPermissions("tlDecCustom:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TlDecCustomSub cond,
			String conditions){
		
		cond = setSeachSql(cond, conditions);
	
		return commonApiService.searchLay(cond);
	}
	

	
	/*
	 * 导出
	 */
	@RequiresPermissions("tlDecCustom:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TlDecCustomSub cond,
			String conditions) throws Exception{
		
		cond = setSeachSql(cond, conditions);
		
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TL_DecCustom)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TlDecCustomSub> util = new ExportExcelUtils<TlDecCustomSub>(new TlDecCustomSub());
        
        String toFilePath = util.exportExcel(listData, listField, "报关信息管理");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TlDecCustomSub setSeachSql(TlDecCustomSub cond, String conditions){

		cond.addLeftJoin("TL_DecCustomBoxProduct D ON D.id = A.DecCustomBoxProductId")
				.addField("D.Num numBox")
    	.addLeftJoin("TL_DecCustomProduct B ON B.Id = D.DecCustomProductId")

    	.addLeftJoin("TL_DecCustom C ON C.Bid = B.Pbid")
    	.addField("C.Bid bid")

		.addLeftJoin("TL_DecCustomBox E ON E.BoxCode = D.BoxCode")
		.addField("E.BoxCode boxCode")
		.addField("E.BidFbaBox bidFbaBox")
    	
    	.addLeftJoin("TP_FbaLogsticsOrder AA ON AA.Id = C.FbaLogsticsOrderId")
	    	.addField("AA.Bid bidFbaLogsticsOrder")
	    	.addField("AA.WaybillNumber waybillNumber")
	    	.addField("AA.DestinationFulfillmentCenterId destinationFulfillmentCenterId")
	    	.addField("AA.NameLogsticsWay nameLogsticsWay")
	    	.addField("AA.CodeLogsticsServer codeLogsticsServer")
    	.addLeftJoin("TP_AmazonAddress BB ON BB.DestinationFulfillmentCenterId = AA.DestinationFulfillmentCenterId")
    		.addField("BB.CountryCode countryCode")
    	
    	.addLeftJoin("TP_FbaPlanOrderDetail AAA ON AAA.Id = B.PlanOrderDetailId")
    		.addField("AAA.CodePlanOrder bidFbaPlanOrder")
    		.addField("AAA.CommodityCode productCode")
    		.addField("AAA.Name productName")


    		
    	.addLeftJoin("TP_FbaLogsticsOrderDetail F ON F.Bid = E.BidFbaBox")
	    	.addField("F.ShipmentId shipmentId")
	    	.addField("F.TrackingCode trackingCode")
	    	
	    .addLeftJoin("TCG_PurchaseAppointOrderProductComponent AAAA ON AAAA.Id = A.ComponentId")
	    	.addField("AAAA.SubCode subCode")
	    	.addField("AAAA.SubName subName")
	    	.addField("AAAA.SubCodeMerge subCodeMerge")
	    	.addField("AAAA.SubBomNumber subBomNumber")
	    	.addField("AAAA.SubBomPrice subBomPrice")
	    	.addField("(AAAA.SubBomNumber * D.Num) subBoxNumber")
	    .addLeftJoin("TCG_PurchaseContract BBBB ON BBBB.Id = A.ContractId")
	    .addLeftJoin("TB_CompanyBankInfo CCCC ON CCCC.Id = BBBB.CompanyBankInfoId")
	    .addLeftJoin("TB_CompanyInfo DDDD ON DDDD.Vid = CCCC.CompanyInfoVid")
			.addField("DDDD.Vname companyVname")
			
		.addLeftJoin("TCW_PartnerBankInfo EEEE ON EEEE.Id = BBBB.PartnerBankInfoId")
			.addField("EEEE.PartnerName partnerName");
		
        return  cond;
    }
}
